package com.xiaojiang.service.impl;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xiaojiang.dto.HouseholdersQueryDto;
import com.xiaojiang.dto.UpdateHouseholderCarDto;
import com.xiaojiang.entity.Householders;
import com.xiaojiang.mapper.HouseholdersMapper;
import com.xiaojiang.service.HouseholdersService;
import com.xiaojiang.vo.HouseholdersQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：小江
 * 日期：2024年10月22日
 */
@Service
public class HouseholdersServiceImpl implements HouseholdersService {

    @Autowired
    private HouseholdersMapper householdersMapper;

    @Override
    public SaResult queryHouseholderByCard(String householderCard) {
        if (householderCard == null || householderCard.isEmpty()){
            return SaResult.error("查询失败！householderCard 不能为空");
        }
        QueryWrapper<Householders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("householder_card", householderCard);
        Householders householder = householdersMapper.selectOne(queryWrapper);
        return SaResult.ok().setData(householder);
    }

    @Override
    public SaResult queryHouseholders(HouseholdersQueryDto householdersQueryDto) {
        List<HouseholdersQueryVo> householdersQueryVos = householdersMapper.queryHouseholders(householdersQueryDto);
        Long total = householdersMapper.countAllExistenceHouseholder();
        if (householdersQueryVos == null || total == 0){
            return SaResult.error("查询失败!");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("householdersQueryVos", householdersQueryVos);
        return SaResult.ok("查询成功！").setData(map);
    }

    @Override
    public SaResult addHouseholder(Householders householders) {
        householdersMapper.insert(householders);
        return SaResult.ok();
    }

    @Override
    public SaResult deleteHouseholderById(Integer householderId) {
        if (householderId == null) {
            return SaResult.error("删除失败！");
        }
        Householders householder = householdersMapper.selectById(householderId);
        if (householder == null) {
            return SaResult.error("用户不存在！");
        }
        if (householder.getIsDelete().equals(1)) {
            return SaResult.error("该用户已经删除！");
        }
        householder.setIsDelete(1);
        int i = householdersMapper.updateById(householder);
        if (i > 0) {
            return SaResult.ok("删除成功！");
        } else {
            return SaResult.error("删除失败！");
        }
    }

    @Override
    public SaResult deleteHouseholderByIds(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return SaResult.error("删除失败！ids 不能为空或为空数组");
        }
        // 构建更新条件
        UpdateWrapper<Householders> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("householder_id", ids)
                .eq("is_delete", 0); // 确保只更新未删除的记录
        // 执行批量更新
        int updateCount = householdersMapper.update(null, updateWrapper.set("is_delete", 1));
        if (updateCount > 0) {
            return SaResult.ok("批量删除成功！");
        } else {
            return SaResult.error("批量删除失败！更新操作未成功执行");
        }
    }

    @Override
    public SaResult updateHouseholder(Householders householders) {
        UpdateWrapper<Householders> updateWrapper = new UpdateWrapper<>();
        // 设置需要更新的字段
        if (householders.getHouseholderName() != null) {
            updateWrapper.set("householder_name", householders.getHouseholderName());
        }
        if (householders.getHouseholderPhone() != null) {
            updateWrapper.set("householder_phone", householders.getHouseholderPhone());
        }
        if (householders.getHouseholderCard() != null) {
            updateWrapper.set("householder_card", householders.getHouseholderCard());
        }
        updateWrapper.set("householder_photo", householders.getHouseholderPhoto());
        updateWrapper.set("car_license_plate", householders.getCarLicensePlate());
        Integer householderId = householders.getHouseholderId();
        if (householderId == null) {
            return SaResult.error("用户ID不能为空");
        }
        updateWrapper.eq("householder_id", householderId);
        int rowsUpdated = householdersMapper.update(null,updateWrapper);
        // 返回结果
        if (rowsUpdated > 0) {
            return SaResult.ok("更新成功!");
        } else {
            return SaResult.error("更新失败!");
        }
    }

    @Override
    public SaResult updateHouseholderCar(UpdateHouseholderCarDto updateHouseholderCarDto) {
        QueryWrapper<Householders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("householder_card", updateHouseholderCarDto.getHouseholderCard());
        try {
            Householders householder = householdersMapper.selectOne(queryWrapper);
            if (householder != null){
                householder.setCarLicensePlate(updateHouseholderCarDto.getCarLicensePlate());
                int i = householdersMapper.updateById(householder);
                if (i > 0) {
                    return SaResult.ok("更新成功！");
                } else {
                    return SaResult.error("更新失败！");
                }
            }else {
                return SaResult.error("用户不存在！");
            }
        }catch (Exception e){
            return SaResult.error("服务异常！");
        }
    }


}
